package cn.lzj.service.base.impl;

import cn.lzj.mapper.IndexMapper;
import cn.lzj.mapper.UserMapper;
import cn.lzj.pojo.ProductBase;
import cn.lzj.pojo.ProductType;
import cn.lzj.pojo.User;
import cn.lzj.service.base.IndexService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class indexServiceImpl implements IndexService {

    @Resource
    private IndexMapper indexMapper;

    @Resource
    private UserMapper userMapper;

    @Override
    public List<ProductType> selectProductType() {
        List<ProductType> productType = indexMapper.getProductType();
        return productType;
    }

    @Override
    public boolean exitShopping(Integer userId) {
        User user = userMapper.selectById(userId);

        if(user!=null) {
            if (user.getUserType() == "user") {
                System.out.println("已经是普通用户了");
                return false;
            }
            //在内存改
            user.setUserType("user");
            //在数据库改
            int count = userMapper.userExitShopping(userId);

            if (count == 1) {
                return true;
            }
        }

        return false;
    }

    @Override
    public Map<Integer, List<ProductBase>> indexPage(Integer startIndex, Integer pageSize, String productName) {
        int pageInfo = indexMapper.getPageInfo();   //  ProductBase数据库总记录数
        int pageNum;                                //  页数
        if(pageInfo % pageSize == 0){
            pageNum = pageInfo / pageSize;
        }else {
            pageNum = (pageInfo / pageSize) + 1;
        }
        Map<Integer, List<ProductBase>> map = new HashMap<>();
        List<ProductBase> list = indexMapper.getProductLimit(startIndex, pageSize,productName);

        map.put(pageNum, list);

        return map;
    }

//    @Override
//    public Map<Integer, List<ProductBase>> indexPage(Integer startIndex, Integer pageSize); {
//        int pageInfo = indexMapper.getPageInfo();
//        int pageNum;
//        if(pageInfo % pageSize == 0){
//            pageNum = pageInfo / pageSize;
//        }else {
//            pageNum = (pageInfo / pageSize) + 1;
//        }
//
//        int pageIndex = (startIndex - 1) * pageSize;
//        List<ProductBase> pageList = new ArrayList<>(pageNum);
//
//
////        pageList.add(pageIndex, );
//
//
//
//    }


    //  返回商品
    @Override
    public List<ProductBase> selectProduct() {
        List<ProductBase> product = indexMapper.getProduct();
        return product;
    }
}
